package luigibros.window;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;


public class LuigiPanel extends JPanel implements ActionListener {

    /**
	 * 
	 */
	private static final long serialVersionUID = 2361376598201794496L;
	private Timer timer;
    private Craft craft;
    private Ghost[] ghosts;
    private Winner w;
    private int starCount;
    public LuigiPanel() {
        addKeyListener(new TAdapter());
        setFocusable(true);
        //repaint();
        ImageIcon ii = new ImageIcon(this.getClass().getResource("mario_rightwalkstep.png"));
        Image image = ii.getImage();
        craft = new Craft(image, this);
        ghosts = new Ghost[10];
        ii = new ImageIcon(this.getClass().getResource("gomba1.png"));
        image = ii.getImage();
        for(int x = 0; x < ghosts.length; ++x) {
        	ghosts[x] = new Ghost(image, this, craft);
        }
        ii = new ImageIcon(this.getClass().getResource("Star.png"));
        image = ii.getImage();
        w = new Winner(image, this, craft);
        starCount = 0;
        timer = new Timer(5, this);
        timer.start();
    }

   public void paintComponent(Graphics g)
    {
    	Graphics2D g2d = (Graphics2D)g;
    	g2d.fillRect(0, 0, getWidth(), getHeight());
        g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);
        for(Ghost gh : ghosts) {
        	g2d.drawImage(gh.getImage(), gh.getX(), gh.getY(), this);
        }
        g2d.drawImage(w.getImage(), w.getX(), w.getY(), this);
    }

    public void actionPerformed(ActionEvent e) {
    	Rectangle windowR = new Rectangle(0, 0, getWidth(), getHeight());
        craft.move();
        for(Ghost gh : ghosts)
        	gh.move(starCount);
        w.move();
        boolean gombaColide = false;
        Rectangle marioR = craft.getRectangle();
        for(Ghost gh : ghosts)
        	gombaColide |= marioR.intersects(gh.getRectangle());
        if(gombaColide) {
        	JOptionPane.showMessageDialog(this.getTopLevelAncestor(), "You just LOST the GAME!  ", "GAME!!!  ", JOptionPane.ERROR_MESSAGE);
        	System.exit(0);
        }
        if(marioR.intersects(w.getRectangle())) {
    		switch(++starCount)
    		{
    		case 1:		JOptionPane.showMessageDialog(this.getTopLevelAncestor(), "You THINK you will WIN, but you will LOSE! Now DODGE!!!! ", "Be Clever!!!  ", JOptionPane.ERROR_MESSAGE);
    			break;
    		case 2:		JOptionPane.showMessageDialog(this.getTopLevelAncestor(), "You THINK you will WIN, but you will LOSE! Now RUN!!!! ", "Go fast!!!  ", JOptionPane.ERROR_MESSAGE);
    			break;
    		case 3:		JOptionPane.showMessageDialog(this.getTopLevelAncestor(), "You THINK you will WIN, but you LOST the GAME!  ", "GAME!!!  ", JOptionPane.ERROR_MESSAGE);
    					System.exit(0);
    			break;
    		}
    		w.jump();
        }
        
        if(!w.getRectangle().intersects(windowR))
        	w.jump();
        repaint();  
    }


    private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            craft.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            craft.keyPressed(e);
        }
    }

}